package com.xd.service.impl;/**
 * Created by mac on 2019/5/21.
 */

import com.xd.dto.AuditDto;
import com.xd.entity.Audit;
import com.xd.entity.IndexImage;
import com.xd.repository.AuditRepository;
import com.xd.repository.IndexImageRepository;
import com.xd.service.AuditService;
import com.xd.util.UploadFile;
import com.xd.vo.AuditVo;
import com.xd.vo.DetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author KANADE_XD
 * @date 2019/5/21 8:47 AM
 * @definition 请输入说明
 */

@Service
public class AuditServiceimpl implements AuditService {
    @Value("${fileUpLoadPath}")
    private String filePath;
    @Resource
    AuditRepository auditRepository;
    @Resource
    IndexImageRepository indexImageRepository;

    @Override
    public List<AuditDto> findall() {
        List<AuditDto> lshumanResourceDto = new ArrayList<AuditDto>();
        List<Audit> lshumanResource = auditRepository.findAll();
        for (Audit humanResource : lshumanResource) {
            AuditDto humanResourceDto = new AuditDto();
            BeanUtils.copyProperties(humanResource, humanResourceDto);
            lshumanResourceDto.add(humanResourceDto);
        }
        return lshumanResourceDto;
    }

    @Override
    public void addmsg(AuditVo auditVo) {
        Audit audit = new Audit();
        System.out.println(auditVo);
        BeanUtils.copyProperties(auditVo, audit);
        auditRepository.save(audit);
    }

    @Override
    public DetailVo getdetail(Long id) {
        Audit audit = auditRepository.findById(id);
        DetailVo detailVo = new DetailVo();
        BeanUtils.copyProperties(audit, detailVo);
        return detailVo;
    }

    @Override
    public void saveIndexImage(HttpServletRequest req, MultipartHttpServletRequest multiReq) {
        IndexImage indexImage = new IndexImage();
        boolean flag = false;
        try {
            String rs = UploadFile.UploadFile(req, multiReq, filePath);
            indexImage.setSrc(rs);
            indexImageRepository.save(indexImage);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        auditRepository.deleteById(id);
    }

}
